//
//  UseLineVC.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/7/24.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "UseLineVC.h"
#import "TopBackView.h"
#import "Common.h"
#import "RootViewController.h"
#import "WKProgressHUD.h"
#import "NullView.h"
#import "CollectionViewCell.h"

@interface UseLineVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NullView *nullView;

@end

@implementation UseLineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [NSMutableArray array];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    TopBackView *topBackView= [[TopBackView alloc]initViewWithFrame:CGRectMake(0, 0, screenWidth, 64) andTitle:@"我的专线"];
    
    topBackView.rightButton.hidden = YES;
    [topBackView.leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:topBackView];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

    self.view.backgroundColor = GrayColor;
    self.nullView = [[NullView  alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight  - 60 * kHeight) andTitle:@"" andImage:[UIImage imageNamed:@"no_lines"]];
    self.nullView.backgroundColor = GrayColor;
    

    [self initDataSource];
    
    [self initTableView];
//    [self initCollectionView];

}

-(void)initDataSource
{
    
    self.nullView.label.text = @"暂无线路";
    
    //城市列表
    [Common requestWithUrlString:sub_routes contentType:application_json finished:^(id responseObj) {
        
        if ([responseObj[@"data"] isEqual:[NSNull null]]) {
            
        } else {
            self.dataArray = [NSMutableArray arrayWithArray:responseObj[@"data"]];
 
        }
        
        if (self.dataArray.count >0) {
           
            [self.nullView removeFromSuperview];

        } else {
            [self.tableView addSubview:self.nullView];

        }
        
        [self.tableView reloadData];
        
        
    } failed:^(NSString *errorMsg) {
        
    }];

}


-(void)initTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.tableView];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 30, 0, 0);
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = @"testCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string];
    }
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.imageView.image = [UIImage imageNamed:@"personal_route"];
   
    if (self.dataArray.count > 0) {
        cell.textLabel.font = Font(13);
        cell.textLabel.textColor = littleBlackColor;
        cell.textLabel.text = self.dataArray[indexPath.row][@"route"];
    }
   
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 43 * kHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]init];

UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 200, 43 * kHeight)];
lable.font = Font(14);
lable.textColor = littleBlackColor;

lable.text = @"常跑线路";
headerView.backgroundColor = GrayColor;
    
    UILabel *linel = [[UILabel alloc]initWithFrame:CGRectMake(0, 43 * kHeight, screenWidth, 0.5)];
    linel.backgroundColor = LineGrayColor;
    [headerView addSubview:linel];

[headerView addSubview:lable];

return headerView;
}




//
//-(void)initCollectionView
//{
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//    layout.itemSize = CGSizeMake(88 * kWidth, 30 * kHeight);
//    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
//    
//    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64) collectionViewLayout:layout];
//    self.collectionView.delegate = self;
//    self.collectionView.dataSource = self;
//    
//    self.collectionView.backgroundColor = WhiteColor;
//    self.collectionView.alwaysBounceVertical = YES;
//    
//    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
//    
//    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
//    
//    [self.view addSubview:self.collectionView];
//}
//
//-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//{
//    return 1;
//}
//
//-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//{
//    
//    
//    if (self.dataArray.count > 0) {
//        
//        return  self.dataArray.count;
//        
//    } else {
//        return 0;
//    }
//    
//}
//
//
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
//    
//    cell.label.layer.borderWidth = 0.5;
//    cell.label.layer.borderColor = LineGrayColor.CGColor;
//    cell.label.font = Font(13);
//    cell.label.text = @"北京";
//    
//    if (self.dataArray.count > 0) {
//        cell.label.text = self.dataArray[indexPath.row][@"route"];
//    }
//    
//    return cell;
//    
//}
//
//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
//        
//        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
//        
//        
//        for (UILabel *view in headerView.subviews) {
//            [view removeFromSuperview];
//        }
//        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 200, headerView.frame.size.height)];
//        lable.font = Font(14);
//        lable.textColor = Color_RGB(14, 14, 14, 1);
//        
//       lable.text = @"我的路线";
//        headerView.backgroundColor = GrayColor;
//        
//        [headerView addSubview:lable];
//        
//        return headerView;
//    } else {
//        return nil;
//    }
//}
//
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    return CGSizeMake(screenWidth, 40);
//}




-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:YES];
    
//    [self initDataSource];
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
